package com.horizon.manage.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.horizon.common.controller.HorizonBaseController;
import com.horizon.common.exception.HorizonException;
import com.horizon.manage.student.dto.StudentDto;
import com.horizon.manage.student.entity.Student;
import com.horizon.manage.student.service.IStudentService;
import com.horizon.manage.student.vo.StudentVO;
import com.horizon.manage.student.converter.StudentEVConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.api.ResultCode;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;
import java.util.Optional;

/**
 * 学生扩展信息controller.
 *
 * @author zhaoshb
 * @since 2022/11/28 23:24
 */
@ApiIgnore
@RestController
@RequestMapping("/horizon/student")
@AllArgsConstructor
@Api(value = "学生扩展信息", tags = "学生扩展信息")
public class StudentController extends HorizonBaseController {

    private IStudentService studentService;

    private StudentEVConverter studentEVConverter;

    @PostMapping("/create")
    @ApiOperation(value = "创建", notes = "创建")
    public R<Long> create(@RequestBody StudentDto studentDto) {
        Optional<Long> optId = studentService.create(studentDto);
        return optId.map(R::data).orElseGet(() -> R.fail(ResultCode.FAILURE));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public R<Object> delete(@RequestBody Long studentExtendInfoId) {
        return R.status(studentService.delete(studentExtendInfoId));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
    public R<Object> update(@RequestBody StudentDto dto) throws HorizonException {
        return R.status(studentService.update(dto));
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "传入cond以及排序条件")
    public R<IPage<StudentVO>> list(@ApiIgnore @RequestParam Map<String, Object> cond, Query query) {
        IPage<Student> pages = studentService.page(Condition.getPage(query), Condition.getQueryWrapper(cond, Student.class));
        return R.data(studentEVConverter.pageVO(pages));
    }
}
